#!/bin/bash

WFMS_BIN_DIR=`dirname "$(cd ${0%/*} && echo $PWD/${0##*/})"`
WFMS_HOME=${WFMS_BIN_DIR%/*}
cd $WFMS_HOME

JARDIRS=(lib lib/everest)

case $(uname) in
    CYGWIN*) PATHSEP=";"
        ;;
    *) PATHSEP=":"
        ;;
esac

JARLIST=""

addToJarList ()
{
    # Params: 1 - directory
    #         2 - archive file extension (jar, zip)
    for arcfile in "$1"/*."$2"; do
        if [ -f "$arcfile" ]; then
            # echo "$arcfile"

            if [ ! -z "$JARLIST" ]; then
                JARLIST="$JARLIST$PATHSEP"
            fi

            JARLIST="$JARLIST$arcfile"
        fi
    done
}

for dir in "${JARDIRS[@]}"; do

    if [ ! -e "$dir" ]; then
        echo "Does not exist: $dir"
    elif [ ! -d "$dir" ]; then
        echo "Not a directory: $dir"
    else
        addToJarList "$dir" "jar"
        addToJarList "$dir" "zip"
    fi
done

if [ "$1" = "ssl" ]; then
    java -classpath $JARLIST -Djava.util.logging.config.file=conf/logging.properties websolve.workflow.wfms.Server conf/jetty-ssl.xml > log/everest.log 2>&1 &
else
    java -classpath $JARLIST -Djava.util.logging.config.file=conf/logging.properties websolve.workflow.wfms.Server conf/jetty.xml > log/everest.log 2>&1 &
fi
echo "$!" > server.pid


